//
//  CCYNetworkTool.swift
//  03.微博
//
//  Created by 蔡承毅 on 16/9/19.
//  Copyright © 2016年 IOS. All rights reserved.
//

import UIKit
import AFNetworking

enum CCYNetworkMethod:String{
    case GET = "GET"
    case POST = "POST"
}

class CCYNetworkTool: NSObject {
    // 单例
    static let shared: CCYNetworkTool = CCYNetworkTool()
    
    // afnManager
    let afnManager:AFHTTPSessionManager
    
    // 重写inite
    fileprivate override init() {
        let urlString = "https://api.weibo.com/2/"
        let url = URL(string: urlString)
        afnManager = AFHTTPSessionManager(baseURL: url)
        afnManager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        super.init()
    }
    // get/post方法
    func request(method: CCYNetworkMethod,URLString: String, parameters: Any?, progress: ((Progress) -> Void)?, success: ((URLSessionDataTask, Any?) -> Void)?, failure: ((URLSessionDataTask?, Error) -> Void)?) -> Void {
        switch method {
        case .GET:
            print("get")
            afnManager.get(URLString, parameters: parameters, progress: progress, success: success, failure: failure)
        case .POST:
            print("post")
            afnManager.post(URLString, parameters: parameters, progress: progress, success: success, failure: failure)
        }
    }
}
